﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.wechat.vip
{
    public partial class ord : System.Web.UI.Page
    {
        v_ord_dal orddal = new v_ord_dal();
        protected v_ord_model ordmodel = new v_ord_model();
        protected DataTable dtord = new DataTable();

        v_vip_dal vipdal = new v_vip_dal();


        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.QueryString["action"])
            {
                case "bindord":
                    submit();
                    break;
            }
         
        }

        private void submit()
        { //

            if (Request.QueryString["wxid"].Trim().ToString() == "null")
            {
                Response.Write("{\"error\":\"请从微信登录！\"}");
                Response.End();
            }

            //if (dtord.Rows.Count > 0)
            //{
            //    ordmodel = orddal.GetModel(int.Parse(dtord.Rows[0]["ord_id"].ToString()));
            //    ordmodel.ord_name = Request.Form["ord_name"].Trim().ToString();
            //    ordmodel.ord_phone = Request.Form["ord_phone"].Trim().ToString();
            //    ordmodel.ord_num = int.Parse(Request.Form["ord_num"].Trim().ToString());
                
            //    ordmodel.user_id = int.Parse(Request.QueryString["uid"].ToString());
            //    ordmodel.wxid = Request.QueryString["wxid"];


            //    orddal.Update(ordmodel);

            //    Response.Write("{\"error\":\"200\",\"msg\":\"\",\"url\":\"index.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "\"}");
            //    Response.End();

            //}
            //else
            //{
                DataTable dt = orddal.GetList("ord_phone='" + Request.Form["ord_phone"] + "'").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    Response.Write("{\"error\":\"电话号码已经注册！\"}");
                    Response.End();
                }
                ordmodel.ord_name = Request.Form["ord_name"].Trim().ToString();
                ordmodel.ord_phone = Request.Form["ord_phone"].Trim().ToString();
                ordmodel.user_id = int.Parse(Request.QueryString["uid"].ToString());
                ordmodel.wxid = Request.QueryString["wxid"];
                ordmodel.ord_time = DateTime.Parse(Request.Form["time"].Trim().ToString());
                if (orddal.Add(ordmodel) > 0)
                {
                    Response.Write("{\"error\":\"200\",\"msg\":\"\",\"url\":\"myord.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "\"}");
                    Response.End();
                }
                else
                {
                    Response.Write("{\"error\":\"提交失败！\"}");
                    Response.End();
                }
           
        }
    }
}